<?php

/* @var $this \yii\web\View */
/* @var $content string */

use yii\helpers\Html;
use front\assets\AppAsset;
use yii\helpers\Url;

?>
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <title>收货地址</title>
    <meta name="viewport" content="width=device-width,initial-scale=1.0,user-scalable=0" />
    <meta name="format-detection" content="telephone=no,email=no" />
    <meta name="mobileOptimized" content="width" />
    <meta name="handheldFriendly" content="true" />
    <meta name="apple-mobile-web-app-capable" content="yes" />
    <meta name="apple-mobile-web-app-status-bar-style" content="black" />
    <link rel="stylesheet" href="/static/css/main.css" />
    <script src="/static/script/zepto.min.js"></script>
</head>

<body class="m-add-adrs m-gds-adrs">
    <div class="list" id="J_forList">
        <?php if($list){?>
            <?php foreach($list as $k=>$v){ ?>
                <div class="u-adrs">
                    <div class="u-adrs-list">
                        <h5 data-addrid="<?php echo $v['id'];?>"><span>收货地址</span><em class="addr-new"><?php echo $v['address'];?></em></h5>
                        <p>收货人: <em class="addr-name"><?php echo $v['name'];?></em><span class="addr-tel"><?php echo $v['mobile'];?></span></p>
                    </div>
                    <a href="javascript:void(0)"  onclick="setAddr(<?php echo $v['id'];?>)" class="u-edit J_editnewAddr" data-id="<?php echo $v['id'];?>">编辑</a>
                </div>
            <?php } ?>
        <?php } ?>
    </div>
	<h4><a href="javascript:void(0)" onclick="addAddr()" class="add" id="J_addnewAddr">添加新的地址</a></h4>
    <h4><a href="javascript:getWxAddress();" class="add" id="use_wx_addr">使用微信地址</a></h4>
    <?php if(empty($list)){?>
        <dl>
            <dt>秀贝想使用您的微信位置信息，以节省您的宝贵时间，您是否同意？</dt>
            <dd id="J_actBtn" class="f-cb">
                <a href="javascript:;">拒绝</a>
                <a href="javascript:;" class="z-agree" id="agree_get_local">同意</a>
            </dd>
        </dl>
    <div class="m-mask" id="J_mask"></div>
    <?php } ?>
<!--    <div class="m-transX" id="J_addAddressInfo">-->
<!---->
<!--        <iframe class="m-iframe" src="/web/order/addaddrinfo" ></iframe>-->
<!---->
<!--    </div>-->

    <div class="m-transX" id="J_editAddressInfo">
        <iframe id="J_editaddrRes" class="m-iframe"></iframe>
    </div>

    <script src="http://res.wx.qq.com/open/js/jweixin-1.0.0.js"></script>

<script>
	$(function(){
		var Jmask = $('#J_mask'), JactBtn = $('#J_actBtn');
		JactBtn.on('touchend', 'a', function(){

			var _this = $(this);
			// 同意
            if(_this.hasClass('z-agree')){
                getWxAddress();
            }

			_this.closest('dl').hide();
			Jmask.hide();
		});

        var transX = 'z-transX';
        $('#J_forList').on('touchend', ".u-adrs-list", function(evt){
            var addr = $(this).find('.addr-new').text(); //地址信息
            var addr_name = $(this).find('.addr-name').text(); //收货人
            var addr_tel = $(this).find('.addr-tel').text();
            var addr_id = $(this).find('h5').attr('data-addrid');
            window.location.href='/web/order/buy?addr_id='+addr_id;
            evt.preventDefault();
            return !1;
        });


//        $('#J_forList').on('touchend','#J_addnewAddr', function(evt){
//            window.location.href='/web/order/addaddrinfo';
//            evt.preventDefault();
//            return !1;
//        });

//        $('.J_editnewAddr').on('touchend', function(evt){
//            window.location.href='/web/order/editaddrinfo?id=' + $(this).attr('data-id');
//            evt.preventDefault();
//            return !1;
//        });


	});

    function addAddr(){
        window.location.href='/web/order/addaddrinfo';
    }

    function setAddr(aid){
        window.location.href='/web/order/editaddrinfo?id=' + aid;
    }

    function getWxAddress() {
        WeixinJSBridge.invoke('editAddress' ,<?=$parameters?> ,function(res){
            if(res.err_msg == 'edit_address:ok'){
                var params = {};
                var transXX = 'z-transX';
                var name = res.userName;
                var mobile = res.telNumber;
                var dist_cn_char = res.proviceFirstStageName + res.addressCitySecondStageName + res.addressCountiesThirdStageName;
                var address = res.addressDetailInfo;
                params.name = name;
                params.mobile = mobile;
                params.dist_cn_char = dist_cn_char;
                params.address = address;
                $.post("", params, function(data) {
                    data = JSON.parse(data);
                    if(data.code == 0) {
                        $(".list").append('<div class="u-adrs">' +
                        '<div class="u-adrs-list">'+
                        '<h5 data-addrid="'+data.result.id+'"><span>收货地址</span><em class="addr-new">' + dist_cn_char + ' ' + address + '</em></h5>' +
                        '<p>收货人:<em class="addr-name">' + name + '</em>' +
                        '<span class="addr-tel">' + mobile + '</span></p>' +
                        '</div>'+
                        '<a href="javascript:void(0)" onclick="setAddr('+data.result.id+')" class="J_editnewAddr u-edit" data-id="' + data.result.id + '">编辑</a>' +
                        '</div>');
                    }else {
                        msgPopup(data.message);
                    }
                });
            }else if(res.err_msg == 'edit_address:cancel'){
                msgPopup("您已取消！");
            }
        });
    }
</script>
</body>